package org.batyrov.equery.action.console;

import org.batyrov.equery.action.BaseAction;
import org.batyrov.equery.action.Const;
import org.batyrov.equery.bean.Window;
import org.batyrov.equery.bean.Service;
import org.batyrov.equery.bean.User;
import org.batyrov.equery.bean.Client;
import org.batyrov.equery.kernel.QueryManager;

import javax.swing.plaf.basic.BasicMenuUI;
import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: root
 * Date: 21.02.2010
 * Time: 18:33:20
 * To change this template use File | Settings | File Templates.
 */
public class GetNextClient extends BaseAction {
    private long windowId = -1;
    private long userId = -1;
    private long serviceId = -1;
    private int clientNumber = -1;
    private String serviceName;
    private String windowName;
    private Collection<Window> windows;
    private Collection<Service> services;
    private Collection<User> users;

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public Collection<User> getUsers() {
        return users;
    }

    public void setUsers(Collection<User> users) {
        this.users = users;
    }

    public Collection<Service> getServices() {
        return services;
    }

    public void setServices(Collection<Service> services) {
        this.services = services;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getWindowName() {
        return windowName;
    }

    public void setWindowName(String windowName) {
        this.windowName = windowName;
    }

    public Collection<Window> getWindows() {
        return windows;
    }

    public void setWindows(Collection<Window> windows) {
        this.windows = windows;
    }

    public long getWindowId() {
        return windowId;
    }

    public void setWindowId(long windowId) {
        this.windowId = windowId;
    }

    public long getServiceId() {
        return serviceId;
    }

    public void setServiceId(long serviceId) {
        this.serviceId = serviceId;
    }

    public int getClientNumber() {
        return clientNumber;
    }

    public void setClientNumber(int clientNumber) {
        this.clientNumber = clientNumber;
    }

    @Override
    public String execute() throws Exception {
        super.execute();
        windows = dao.getAllWindow();
        services = dao.getAllService();
        users = dao.getAllUsers();
        Service service =  em.find(Service.class, serviceId);
        if (service == null){
            addActionError(getText("cannot_found_service"));
            return Const.ERROR;
        }
        Window window = em.find(Window.class, windowId);
        if (window == null){
            addActionError(getText("cannot_found_window"));
            return Const.ERROR;
        }
        Client client = QueryManager.getQueryManager().getNextClient(windowId);
        if (client == null){
            addActionMessage(getText("query_is_empty"));
        }else{
            addActionMessage("???? ????? ??????? ????? " + client.getNumber()+" ?? ???? "+window.getName());
            clientNumber = client.getNumber();
            windowName = window.getName();
            serviceName = service.getName();
        }
        return Const.SUCCESS;
    }
}
